package TorrentEngine;
import java.io.OutputStream;
import java.util.Vector;
import Logger.MTLogger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Queue //implements Runnable
{
	public Vector queue;
        OutputStream os;
       // Thread t;
        int queueType;
	
	Queue(int aQueueType)
	{
		queue=new Vector();
                //t = new Thread(this);
                //t.start();
                queueType=aQueueType;

	}
	synchronized public void enqueue (Object element) throws Exception
	{
		
            queue.addElement (element);
            //System.out.println("En::  "+ element);
		
	}	

	synchronized public int dequeue () throws Exception
	{			
               // if(queue.isEmpty())
               //    System.out.println("___________EMPTY___QUEUE__________");
            while(!queue.isEmpty()){
                byte [] byteArray;
                if(this.queueType==1){
                    byteArray= (byte[]) queue.elementAt(0);
                    os.write(byteArray);
                    os.flush();
                    queue.removeElementAt (0);
                    //return ob;
                    //return -1;
            }
                else{
                    DataMessageTemplate dmt = (DataMessageTemplate) queue.elementAt(0);
                    int aPieceIndex=dmt.aPieceIndex;
                    int aBegin=dmt.aBegin;
                    int aLength=dmt.aLength;
                    MTTorrent torrent=dmt.torrent;
                    MTPeer peer=dmt.peer;

                    MTPiece piece = torrent.piece(aPieceIndex);

                    byte[] block = piece.getBlock(aBegin, aLength);

                    ByteArrayOutputStream bs = new ByteArrayOutputStream();
                    bs.write(putIntToSendBuffer(9 + aLength));
                    bs.write(7); //KMessageIdPiece = 7
                    bs.write(putIntToSendBuffer(aPieceIndex));
                    bs.write(putIntToSendBuffer(aBegin));

                     bs.write(block);
                     bs.flush();
                     os.write(bs.toByteArray());
                     os.flush();
                     bs.close();
                     bs = null;
                     queue.removeElementAt (0);
                     //return 0;
                 }

                    //System.out.println("De: " + byteArray);
                    //Object ob=queue.elementAt(0);
                    
            }
            return 0;
	}

         public void setOPS(OutputStream opStream) throws Exception{
              os=opStream;
              if(os.equals(null))
                  System.out.println("___________NULL OS__________");
      
        }

         private byte[] putIntToSendBuffer(int aInteger)
        {
            byte[] buffer = new byte[4];

            buffer[3] = (byte) (aInteger & 0xFF);
            buffer[2] = (byte) ((aInteger & (0xFF << 8)) >> 8);
            buffer[1] = (byte) ((aInteger & (0xFF << 16)) >> 16);
            buffer[0] = (byte) ((aInteger & (0xFF << 24)) >> 24);

            return buffer;
        }
        public void log(String aText, MTPeer peer)
        {
            MTLogger.writeLine("[Peer " + peer.getAddress() + ":" + peer.getPort() +
                " " + peer.getClient() + "]");
            MTLogger.writeLine("\t" + aText);
        }

   /*public void run() {
      // System.out.println("New Thread!");
     while(true) {
        // System.out.println("Inside While Loop");
	if(!queue.isEmpty())
		try {
                dequeue();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        //else
            //System.out.println("___________Empty____Queue________");
	}
    } */
		       
}